package com.itheima.service.impl;

import com.itheima.entity.TbBusiness;
import com.itheima.dao.TbBusinessDao;
import com.itheima.service.TbBusinessService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 线索(TbBusiness)表服务实现类
 *
 * @author makejava
 * @since 2022-01-16 15:48:53
 */
@Service("tbBusinessService")
public class TbBusinessServiceImpl implements TbBusinessService {
    @Resource
    private TbBusinessDao tbBusinessDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbBusiness queryById(Long id) {
        return this.tbBusinessDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param tbBusiness 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<TbBusiness> queryByPage(TbBusiness tbBusiness, PageRequest pageRequest) {
        long total = this.tbBusinessDao.count(tbBusiness);
        return new PageImpl<>(this.tbBusinessDao.queryAllByLimit(tbBusiness, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param tbBusiness 实例对象
     * @return 实例对象
     */
    @Override
    public TbBusiness insert(TbBusiness tbBusiness) {
        this.tbBusinessDao.insert(tbBusiness);
        return tbBusiness;
    }

    /**
     * 修改数据
     *
     * @param tbBusiness 实例对象
     * @return 实例对象
     */
    @Override
    public TbBusiness update(TbBusiness tbBusiness) {
        this.tbBusinessDao.update(tbBusiness);
        return this.queryById(tbBusiness.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.tbBusinessDao.deleteById(id) > 0;
    }
}
